<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: ActiveResource::HttpMock</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">ActiveResource::HttpMock</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activeresource/lib/active_resource/http_mock_rb.html">
                vendor/rails/activeresource/lib/active_resource/http_mock.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="../Object.html">
                Object
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
One thing that has always been a pain with remote web services is testing.
The <a href="HttpMock.html">HttpMock</a> class makes it easy to test your
Active Resource models by creating a set of mock <a
href="HttpMock.html#M001227">responses</a> to specific <a
href="HttpMock.html#M001226">requests</a>.
</p>
<p>
To test your Active Resource model, you simply call the <a
href="HttpMock.html#M001228">ActiveResource::HttpMock.respond_to</a> method
with an attached block. The block declares a set of URIs with expected
input, and the output each request should return. The passed in block has
any number of entries in the following generalized format:
</p>
<pre>
  mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {})
</pre>
<ul>
<li><tt>http_method</tt> - The HTTP method to listen for. This can be
<tt>get</tt>, <tt>post</tt>, <tt>put</tt>, <tt>delete</tt> or
<tt>head</tt>.

</li>
<li><tt>path</tt> - A string, starting with a &quot;/&quot;, defining the URI
that is expected to be called.

</li>
<li><tt>request_headers</tt> - Headers that are expected along with the
request. This argument uses a hash format, such as <tt>{
&quot;Content-Type&quot; =&gt; &quot;application/xml&quot; }</tt>. This
mock will only trigger if your tests sends a request with identical
headers.

</li>
<li><tt>body</tt> - The data to be returned. This should be a string of Active
Resource parseable content, such as XML.

</li>
<li><tt>status</tt> - The HTTP response code, as an integer, to return with the
response.

</li>
<li><tt>response_headers</tt> - Headers to be returned with the response. Uses
the same hash format as <tt>request_headers</tt> listed above.

</li>
</ul>
<p>
In order for a mock to deliver its content, the incoming request must match
by the <tt>http_method</tt>, <tt>path</tt> and <tt>request_headers</tt>. If
no match is found an <a
href="InvalidRequestError.html">InvalidRequestError</a> exception will be
raised letting you know you need to create a new mock for that request.
</p>
<h4>Example</h4>
<pre>
  def setup
    @matz  = { :id =&gt; 1, :name =&gt; &quot;Matz&quot; }.to_xml(:root =&gt; &quot;person&quot;)
    ActiveResource::HttpMock.respond_to do |mock|
      mock.post   &quot;/people.xml&quot;,   {}, @matz, 201, &quot;Location&quot; =&gt; &quot;/people/1.xml&quot;
      mock.get    &quot;/people/1.xml&quot;, {}, @matz
      mock.put    &quot;/people/1.xml&quot;, {}, nil, 204
      mock.delete &quot;/people/1.xml&quot;, {}, nil, 200
    end
  end

  def test_get_matz
    person = Person.find(1)
    assert_equal &quot;Matz&quot;, person.name
  end
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001226">requests</a>&nbsp;&nbsp;
      <a href="#M001229">reset!</a>&nbsp;&nbsp;
      <a href="#M001228">respond_to</a>&nbsp;&nbsp;
      <a href="#M001227">responses</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M001226" class="method-detail">
        <a name="M001226"></a>

        <div class="method-heading">
          <a href="#M001226" class="method-signature">
          <span class="method-name">requests</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns an array of all request objects that have been sent to the mock.
You can use this to check if your model actually sent an HTTP request.
</p>
<h4>Example</h4>
<pre>
  def setup
    @matz  = { :id =&gt; 1, :name =&gt; &quot;Matz&quot; }.to_xml(:root =&gt; &quot;person&quot;)
    ActiveResource::HttpMock.respond_to do |mock|
      mock.get &quot;/people/1.xml&quot;, {}, @matz
    end
  end

  def test_should_request_remote_service
    person = Person.find(1)  # Call the remote service

    # This request object has the same HTTP method and path as declared by the mock
    expected_request = ActiveResource::Request.new(:get, &quot;/people/1.xml&quot;)

    # Assert that the mock received, and responded to, the expected request from the model
    assert ActiveResource::HttpMock.requests.include?(expected_request)
  end
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001226-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001226-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/http_mock.rb, line 88</span>
88:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">requests</span>
89:         <span class="ruby-ivar">@@requests</span> <span class="ruby-operator">||=</span> []
90:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001229" class="method-detail">
        <a name="M001229"></a>

        <div class="method-heading">
          <a href="#M001229" class="method-signature">
          <span class="method-name">reset!</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Deletes all logged <a href="HttpMock.html#M001226">requests</a> and <a
href="HttpMock.html#M001227">responses</a>.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001229-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001229-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/http_mock.rb, line 115</span>
115:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">reset!</span>
116:         <span class="ruby-identifier">requests</span>.<span class="ruby-identifier">clear</span>
117:         <span class="ruby-identifier">responses</span>.<span class="ruby-identifier">clear</span>
118:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001228" class="method-detail">
        <a name="M001228"></a>

        <div class="method-heading">
          <a href="#M001228" class="method-signature">
          <span class="method-name">respond_to</span><span class="method-args">(pairs = {}) {|mock| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Accepts a block which declares a set of <a
href="HttpMock.html#M001226">requests</a> and <a
href="HttpMock.html#M001227">responses</a> for the <a
href="HttpMock.html">HttpMock</a> to respond to. See the main <a
href="HttpMock.html">ActiveResource::HttpMock</a> description for a more
detailed explanation.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001228-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001228-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/http_mock.rb, line 101</span>
101:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">respond_to</span>(<span class="ruby-identifier">pairs</span> = {}) <span class="ruby-comment cmt">#:yields: mock</span>
102:         <span class="ruby-identifier">reset!</span>
103:         <span class="ruby-identifier">pairs</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">response</span>)<span class="ruby-operator">|</span>
104:           <span class="ruby-identifier">responses</span>[<span class="ruby-identifier">path</span>] = <span class="ruby-identifier">response</span>
105:         <span class="ruby-keyword kw">end</span>
106: 
107:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
108:           <span class="ruby-keyword kw">yield</span> <span class="ruby-constant">Responder</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">responses</span>)
109:         <span class="ruby-keyword kw">else</span>
110:           <span class="ruby-constant">Responder</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">responses</span>)
111:         <span class="ruby-keyword kw">end</span>
112:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001227" class="method-detail">
        <a name="M001227"></a>

        <div class="method-heading">
          <a href="#M001227" class="method-signature">
          <span class="method-name">responses</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a hash of <tt>request =&gt; response</tt> pairs for all all <a
href="HttpMock.html#M001227">responses</a> this mock has delivered, where
<tt>request</tt> is an instance of <a
href="Request.html">ActiveResource::Request</a> and the response is,
naturally, an instance of <a
href="Response.html">ActiveResource::Response</a>.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001227-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001227-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/http_mock.rb, line 95</span>
95:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">responses</span>
96:         <span class="ruby-ivar">@@responses</span> <span class="ruby-operator">||=</span> {}
97:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>